package com.zth.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.zth.pojo.Category;
import com.zth.service.CategoryService;
import com.zth.util.FileUtil;
import com.zth.util.Result;

@RestController
@RequestMapping("/category")
public class CategoryController {
	@Autowired
	CategoryService categoryService;
	
	private String url;
	

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	@RequestMapping("/CategoryAll")
	public Map<String, Object> CategoryAll(@RequestParam(defaultValue ="50",name = "page")int page,@RequestParam(defaultValue = "10",name = "limit")int pageSize){
		List<Category> category=categoryService.CategoryAll(page,pageSize);
		Map<String, Object> map=new HashMap<String, Object>();
		PageInfo<Category> info=new PageInfo<>(category);
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", info.getList());
		map.put("count", info.getTotal());
		return map;
	}
	
	@RequestMapping("/findByName")
	Category findByName(String blog_category_name) {
		return categoryService.findByName(blog_category_name);
				
	}
	@RequestMapping("/findById")
	Category findById(int category_id) {
		return categoryService.findById(category_id);
	}
	
	@RequestMapping("/edit")
	public Map<String, Object> edit(Category category){
		category.setCategory_icon(getUrl());
		LocalDateTime dateTime=LocalDateTime.now();
		category.setCreate_time(dateTime);
		int row=categoryService.edit(category);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("message", "编辑");
		map.put("row", row);
		return map;
	} 
	
	@RequestMapping("/add")
	public Map<String, Object> add(Category category){
		System.out.println(category);
		LocalDateTime dateTime=LocalDateTime.now();
		category.setCategory_icon("/ssb/uploads/d7c90553-ba06-44f7-971a-a8cd039d6dc9.jpg");
		category.setCreate_time(dateTime);
		int row=categoryService.add(category);
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("code", 0);
		map.put("succeed", "添加");
		map.put("row", row);
		return map;
	}
	
	@RequestMapping("/deleteBatch")
	public int deleteBatch(@RequestParam(name = "ids[]")int[] ids) {
		
		return categoryService.deleteBatch(ids);
	}
	
	@RequestMapping("/upload")
	//文件上传
	public Map<String, Object> upload(MultipartFile file){
		String files=FileUtil.getFileuploading(file, "D:/uploads/"); 
		setUrl("/ssb"+files);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("data", "/ssb"+files);
		map.put("code", 0);
		map.put("msg", "");
		return map;
	}


}
